import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login'
import Home from '../components/home'
import UserList from '../components/user'
import UserDetail from '../components/userDetail'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/user',
    component: Home,
    children: [{
      path: '/user',
      component: UserList,
    }, {
      path: '/userDetail',
      component: UserDetail,
    }]
  }
]

const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 路由守卫
router.beforeEach((to, form, next) => {
  if (to.path === '/login') return next()
  const id = sessionStorage.getItem("id")
  if (!id) return next("/login")
  next()
})
export default router
